package org.yak.core.web;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.yak.core.store.Identifiable;

import com.github.pagehelper.PageInfo;

/**
 * Ajax处理结果对象
 * @author zhouyang
 * @param <T>
 * @date 2014年3月7日下午4:08:52
 */
public class Result implements Serializable {
	/**
	 * @fields serialVersionUID
	 */
	private static final long serialVersionUID = 5905715228490291386L;
	/**
	 * @fields status  状态信息，正确返回OK，否则返回 ERROR，如果返回ERROR则需要填写Message信息
	 */
	private Status status;
	/**
	 * @fields record 消息对象
	 */
	private String message;

	/**
	 * @fields Rows 返回结果集
	 */
	private List<?> list;

	/**
	 * @fields Total 返回结果集总数
	 */
	private long recordsTotal = 0;
	private long recordsFiltered = 0;


	private PageInfo<?> pageInfo;

	private Object entity;

	public Result() {
		super();
	}

	/**
	 * @description
	 * @param status 状态
	 * @param message 消息
	 */
	public Result(Status status, String message) {
		this.status = status;
		this.message = message;
	}

	public Result(Status status, List<?> list) {
		this.status = status;
		this.list = list;
	}

	public Result(Status status, String message,PageInfo<?> pageInfo) {
		this.status = status;
		this.message = message;
		this.list = pageInfo.getList();
		this.recordsTotal = pageInfo.getTotal();
		this.recordsFiltered = pageInfo.getTotal();
		this.pageInfo = pageInfo;
		this.pageInfo.setList(null);

	}
	
	public Result(Status status,String message,Object entity) {
		this.status = status;
		this.message = message;
		this.entity = entity;
	}
	
	/**
	 * 返回执行结果，结果中包含实体
	 * @param status
	 * @param message
	 * @param entity
	 */
	public Result(Status status, String message,Identifiable entity) {
		this.status = status;
		this.message = message;
		this.entity = entity;
	}
	
	public Result(Status status, String message,Map<String,Object> entity) {
		this.status = status;
		this.message = message;
		this.entity = entity;
	}

	/**
	 * 结果类型信息
	 * @author zhouyang
	 * @date 2014年3月7日下午4:20:23
	 */
	public enum Status {
		OK, ERROR,SESSION_TIMEOUT
	}

	/**
	 * 添加成功结果信息
	 * @param record
	 */
	public void addOK(String message) {
		this.message = message;
		this.status = Status.OK;
	}

	/**
	 * 添加错误消息
	 * @param message
	 */
	public void addError(String message) {
		this.message = message;
		this.status = Status.ERROR;
	}

	public List<?> getList() {
		return list;
	}

	public long getTotal() {
		return recordsTotal;
	}


	public long getRecordsFiltered() {
		return recordsFiltered;
	}

	public long getRecordsTotal() {
		return recordsTotal;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public Object getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public PageInfo<?> getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo<?> pageInfo) {
		this.pageInfo = pageInfo;
	}

	public Object getEntity() {
		return entity;
	}

	public void setEntity(Object entity) {
		this.entity = entity;
	}
}
